package cn.afterturn.easypdf.util;

import technology.tabula.TextElement;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * PDF 垂直线
 * 配合ExtractionAlgorithm 绘制Excel表格时候使用
 *
 * @since 202405
 */
public class VerticalRulingUtil {


    /**
     * 获取表头左对齐的方式,获取明细列表
     *
     * @param list
     * @param words
     * @return
     */
    public static List<Float> getVerticalsByLeft(List<TextElement> list, String[] words) {
        return getVerticalsByLeft(list, words, 2);
    }


    public static List<Float> getVerticalsByLeft(List<TextElement> list, String[] words, double leftSize) {
        List<Float> result = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getText().equals(words[i].substring(0, 1))) {
                    if (words[i].equals(getText(list, j, j + words[i].length()))) {
                        result.add((float) (list.get(j).getMinX() - leftSize));
                        break;
                    }
                }
            }
        }
        Collections.sort(result);
        return result;
    }


    /**
     * 获取表头左对齐的方式,获取明细列表
     *
     * @param list
     * @param words
     * @return
     */
    public static List<Float> getVerticalsByRight(List<TextElement> list, String[] words) {
        return getVerticalsByRight(list, words, 2);
    }


    public static List<Float> getVerticalsByRight(List<TextElement> list, String[] words, double leftSize) {
        List<Float> result = new ArrayList<>();
        for (int i = 0; i < words.length; i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getText().equals(words[i].substring(0, 1))) {
                    if (words[i].equals(getText(list, j, j + words[i].length()))) {
                        result.add((float) (list.get(j + words[i].length() - 1).getMaxX() + leftSize));
                        break;
                    }
                }
            }
        }
        Collections.sort(result);
        return result;
    }

    private static String getText(List<TextElement> list, int start, int end) {
        String str = list.get(start).getText();
        for (int i = start + 1; i < end; i++) {
            str += list.get(i).getText();
        }
        return str;
    }


    /**
     * 获取表头左对齐的方式,获取明细列表
     *
     * @param list
     * @param words
     * @return
     */
    public static List<Float> getVerticalsByCenter(List<TextElement> list, String[] words) {
        List<Float> result = new ArrayList<>();
        for (int i = 0; i < words.length - 1; i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getText().equals(words[i].substring(0, 1))) {
                    if (words[i].equals(getText(list, j, j + words[i].length()))) {
                        // 找到了第一个,找下一个字符串
                        boolean isFind = false;
                        for (int k = j + words[i].length(); k < list.size(); k++) {
                            if (list.get(k).getText().equals(words[i + 1].substring(0, 1))) {
                                if (words[i + 1].equals(getText(list, k, k + words[i + 1].length()))) {
                                    result.add((float) ((list.get(j + words[i].length() - 1).getMaxX() + list.get(k).getMinX())) / 2);
                                    isFind = true;
                                    break;
                                }
                            }
                        }
                        if (isFind) {
                            break;
                        }

                    }
                }
            }
        }
        Collections.sort(result);
        return result;
    }

    public static List<Float> getVerticalsByCenter(List<TextElement> list, String[] words, double leftSize) {
        List<Float> result = new ArrayList<>();
        for (int i = 0; i < words.length - 1; i++) {
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getText().equals(words[i].substring(0, 1))) {
                    if (words[i].equals(getText(list, j, j + words[i].length()))) {
                        // 找到了第一个,找下一个字符串
                        boolean isFind = false;
                        for (int k = j + words[i].length(); k < list.size(); k++) {
                            if (list.get(k).getText().equals(words[i + 1].substring(0, 1))) {
                                if (words[i + 1].equals(getText(list, k, k + words[i + 1].length()))) {
                                    result.add((float) ((list.get(j + words[i].length() - 1).getMaxX() + leftSize + list.get(k).getMinX())) / 2);
                                    isFind = true;
                                    break;
                                }
                            }
                        }
                        if (isFind) {
                            break;
                        }

                    }
                }
            }
        }
        Collections.sort(result);
        return result;
    }

}
